package edu.uestc.fly.performancetest.mem;

import android.os.AsyncTask;

import edu.uestc.fly.performancetest.constant.Constant;
import edu.uestc.fly.performancetest.utils.ProgressListen;

/**
 * Created by flyln on 2017/10/23 15:42.
 * email:xyf_uestc@163.com
 *
 * String*********对应我们的URL类型
 * Integer********进度条的进度值
 * BitMap*********异步任务完成后返回的类型
 * 
 */
public class MEMAsyncTask extends AsyncTask<String, Integer, Double> {

    public ProgressListen progresslistener;
    public Constant.TaskType taskType = Constant.TaskType.MEM;

    //执行异步任务（doInBackground）之前执行，并且在ui线程中执行
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();


        if (progresslistener != null){
            progresslistener.initProgress(taskType,0);
        }


    }

    @Override
    protected Double doInBackground(String... params) {
        // TODO Auto-generated method stub
        int size =1024*1024*5;
        int i,length = size;
        char oldArray[] = new char[size];
        char newArray[] = new char[size];
        for( i = 0 ; i < size ; i++ )
            oldArray[i]=(char) i;

        long StartTime = System.currentTimeMillis();
        int copyTimes = 100;
        int searchTimes = 100;


        int totalSize = copyTimes + searchTimes;
        int totalCount = 0;

        for( i = 0 ; i < copyTimes;i++ )
        {

            int startPosNew = 0;
            int startPosOld = 0;
            for (int j = 0; j < oldArray.length; j++)
            {
                newArray[startPosNew++] =  oldArray[startPosOld++];
            }
            totalCount ++;
            publishProgress((int)( (float)totalCount / (float)totalSize * 100 ));
                      //100次2475 1000次19290
            //System.arraycopy(oldArray, 0, newArray, 0, length);//        940       10225
            //newArray=oldArray.clone();                        //        2545          25425

            //selfCopy(newArray, 0, newArray, 1, length-1);
            //System.arraycopy(newArray, 0, newArray, 1, length-1);
        }


        int findCount = 0;
        for (int k = 0 ; k < searchTimes ; k++){
            int index = (int) (Math.random() * size);
            for (int m = 0; m < newArray.length ; m++){
                if (newArray[m] == (char)index){
                    findCount++ ;
                    totalCount++;
                    publishProgress((int)( (float)totalCount / (float)totalSize * 100 ));
                    break;
                }
            }


        }
        assert findCount == 100;


        long EndTime = System.currentTimeMillis();

        System.out.println("消耗时间: " + (EndTime - StartTime)+" ms.");
        System.out.println(new String(oldArray).substring(100,200)+"\n\n"+new String(newArray).substring(100,200));



        return (double)EndTime - StartTime;
    }
    //在ui线程中执行 可以操作ui
    @Override
    protected void onPostExecute(Double runTime) {
        // TODO Auto-generated method stub
        super.onPostExecute(runTime);
        //任务完成，对话框进度条隐藏

        if (progresslistener != null){
            progresslistener.closeProgress(taskType,runTime);
        }
    }
    /*
     * 在doInBackground方法中已经调用publishProgress方法 更新任务的执行进度后
     * 调用这个方法 实现进度条的更新
     * */
    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        if (progresslistener != null){
            progresslistener.updateProgress(taskType,values[0]);
        }
    }

}